package com.zzptc.tkfate;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.zzptc.tkfate.entiy.User;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    Button btn_lo;
    Button btn_gt;
    EditText en_ho;
    EditText en_yu;
    private static final String TAG = "MainActivity";
    //List<User> list=new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        init();

        event();
    }

    public void event() {
        btn_lo.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Toast.makeText(MainActivity.this, "hollo", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(MainActivity.this, RegActivity.class);
                startActivityForResult(intent, 1);
            }
        });
        btn_gt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String usenhot = en_ho.getText().toString().trim();
                String password = en_yu.getText().toString().trim();

                //到xml文件中
                SharedPreferences sp =getSharedPreferences("data",MODE_PRIVATE);
                String name = sp.getString("usenhot","未获取到数据");

            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1 && requestCode == 3) {
            //取值
            String usenho = data.getStringExtra("usenho");
            String yu = data.getStringExtra("yu");
            Log.d(TAG, "回传" + usenho);
            Log.d(TAG, "回传" + yu);
            //封装数据
            User user=new User(usenho,yu);
            reg(user);

        }
    }

    public void reg(User user){
        SharedPreferences sp = getSharedPreferences("data",MODE_PRIVATE);
        SharedPreferences.Editor editor = sp.edit();
        editor.putString("usenhot",user.getUsenhot());
        editor.putString("password",user.getPassword());
        editor.commit();


        //判断集合中有没有对象或有没有该对象
     /*   if(list.size()==0){
            list.add(user);
            Log.d(TAG,list.size()+"==="+user.getUsenhot());
        }else{
            for (User s:list){
                if (s.getUsenhot().equals(user.getUsenhot())){
                    Toast.makeText(this, "该用户已存在", Toast.LENGTH_SHORT).show();
                    break;
                }else{
                    list.add(user);
                    Log.d(TAG,list.size()+"==="+user.getUsenhot());
                }

            }
        }*/


    }

        public void init() {
            btn_lo = findViewById(R.id.btn_lo);
            btn_gt = findViewById(R.id.btn_gt);
            en_ho = findViewById(R.id.en_ho);
            en_yu = findViewById(R.id.en_yu);


        }

    }
